<template>
  <div class="inventoryTransact">
    <div class="publicSearch">
      <div class="searchBar" :style="{ height: isMoreSearch ? 'auto' : '' }">
        <el-button-group>
          <el-button
            @click="getInventoryTransactData"
            size="mini"
            icon="el-icon-search"
            >查询</el-button
          >
          <el-button @click="clearSearchData" size="mini" icon="el-icon-refresh"
            >重置</el-button
          >
        </el-button-group>
        <div>
          <span>事务类型</span>
          <el-select
            v-model="searchData.transactType"
            size="mini"
            filterable
            placeholder="请选择事务类型"
            clearable
          >
            <el-option
              v-for="item in transactTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <div>
          <span>物料编码</span>
          <el-input
            v-model="searchData.itemCode"
            size="mini"
            placeholder="物料编码"
            clearable
          ></el-input>
        </div>
        <div>
          <span>物料名称</span>
          <el-input
            v-model="searchData.itemName"
            size="mini"
            placeholder="物料名称"
            clearable
          ></el-input>
        </div>
        <div>
          <span>批次</span>
          <el-input
            v-model="searchData.batchName"
            size="mini"
            placeholder="批次"
            clearable
          ></el-input>
        </div>
        <div>
          <span>追踪号</span>
          <el-input
            v-model="searchData.boxCode"
            size="mini"
            placeholder="追踪号"
            clearable
          ></el-input>
        </div>
        <!-- <div>
          <span>单据类型</span>
          <el-select
            v-model="searchData.billType"
            size="mini"
            filterable
            placeholder="请选择单据类型"
            clearable
          >
            <el-option
              v-for="item in billTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div> -->
        <!-- <div>
          <span>原因</span>
          <el-input
            v-model="searchData.reason"
            size="mini"
            placeholder="原因"
            clearable
          ></el-input>
        </div> -->
        <div>
          <span>单据号</span>
          <el-input
            v-model="searchData.detailNo"
            size="mini"
            placeholder="单据号"
            clearable
          ></el-input>
        </div>
        <!-- <div>
          <span>状态</span>
          <el-select
            v-model="searchData.state"
            size="mini"
            filterable
            placeholder="请选择状态"
            clearable
          >
            <el-option :key="1" label="完成" :value="1"></el-option>
            <el-option :key="-1" label="撤回" :value="-1"></el-option>
          </el-select>
        </div> -->
        
        
        <div>
          <span>来源货位</span>
          <el-input
            v-model="searchData.fromCellCode"
            size="mini"
            placeholder="来源货位"
            clearable
          ></el-input>
        </div>

        <!-- <div>
          <span>调入批次名称</span>
          <el-input
            v-model="searchData.toBatchName"
            size="mini"
            placeholder="调入批次名称"
            clearable
          ></el-input>
        </div>
        <div>
          <span>调入追踪号</span>
          <el-input
            v-model="searchData.toBoxCode"
            size="mini"
            placeholder="调入追踪号"
            clearable
          ></el-input>
        </div> -->
        <div>
          <span>目标货位</span>
          <el-input
            v-model="searchData.toCellCode"
            size="mini"
            placeholder="目标货位"
            clearable
          ></el-input>
        </div>

        <div>
          <span>创建人</span>
          <el-input
            v-model="searchData.createUserName"
            size="mini"
            placeholder="创建人"
            clearable
          ></el-input>
        </div>

        <div>
          <span>创建时间</span>
          <el-date-picker
            v-model="searchData.createTimeArr"
            size="mini"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>
      </div>
      <span
        class="moreSearchBtn"
        :class="isMoreSearch ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"
        @click="isMoreSearch = !isMoreSearch"
      ></span>
    </div>
    <div class="headerBtns" style="margin-top: 30px;"></div>
    <div class="tableBox">
      <el-table
        highlight-current-row
        :data="inventoryTransacts"
        style="width: 100%"
      >
        <el-table-column fixed="left" type="index" width="50">
        </el-table-column>
        <el-table-column
          sortable
          prop="transactType"
          label="事务类型"
          width="100"
        >
          <template slot-scope="scope">
            <span
              v-for="(item, index) in transactTypes"
              :key="index"
              v-if="scope.row.transactType === item.value"
              >{{ item.label }}</span
            >
          </template>
        </el-table-column>

        <el-table-column
          sortable
          show-overflow-tooltip
          prop="itemCode"
          label="物料编码"
          width="150"
        >
          <!-- <template slot-scope="scope">
            <span
              class="textBtn"
              @click="
                $EventBus.$emit('showPublicModal', scope.row.itemCode, 'item')
              "
              >{{ scope.row.itemCode }}</span
            >
          </template> -->
        </el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="itemName"
          label="物料名称"
          width="150"
        >
          <template slot-scope="scope">
            <span
              class="textBtn"
              @click="
                $EventBus.$emit('showPublicModal', scope.row.itemCode, 'item')
              "
              >{{ scope.row.itemName }}</span
            >
          </template>
        </el-table-column>
        <!-- <el-table-column
          sortable
          show-overflow-tooltip
          prop="wareName"
          label="仓库"
          width="150"
        >
          <template slot-scope="scope">
            <span
              class="textBtn"
              @click="
                $EventBus.$emit('showPublicModal', scope.row.wareId, 'ware')
              "
              >{{ scope.row.wareName }}</span
            >
          </template>
        </el-table-column> -->
        <!-- <el-table-column
          sortable
          show-overflow-tooltip
          prop="fromOrganizationName"
          label="货主"
          width="180"
        >
          <template slot-scope="scope">
            <span
              class="textBtn"
              @click="
                $EventBus.$emit(
                  'showPublicModal',
                  scope.row.fromOrganizationId,
                  'organization'
                )
              "
              >{{ scope.row.fromOrganizationName }}</span
            >
            <br />
            <span
              class="textBtn"
              @click="
                $EventBus.$emit(
                  'showPublicModal',
                  scope.row.toOrganizationId,
                  'organization'
                )
              "
              >{{ scope.row.toOrganizationName }}</span
            >
          </template>
        </el-table-column> -->
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="fromBatchName"
          label="批次"
          width="180"
        >
          <template slot-scope="scope">
            <span> {{ scope.row.fromBatchName }}</span>
            <br />
            <span> {{ scope.row.toBatchName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="fromBoxCode"
          label="追踪号"
          width="150"
        >
          <template slot-scope="scope">
            <span> {{ scope.row.fromBoxCode }}</span>
            <br />
            <span> {{ scope.row.toBoxCode }}</span>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="CellCode"
          label="货位编码"
          width="150"
        >
          <template slot-scope="scope">
            <span> {{ scope.row.fromCellCode }}</span>
            <br />
            <span> {{ scope.row.toCellCode }}</span>
          </template>
        </el-table-column>
        
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="fromQuantity"
          label="数量"
          width="150"
        >
          <template slot-scope="scope">
            <span> {{ scope.row.fromQuantity }}</span>
            <br />
            <span> {{ scope.row.toQuantity }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column
          sortable
          show-overflow-tooltip
          prop="fromPackDescribe"
          label="单位"
          width="150"
        >
          <template slot-scope="scope">
            <span> {{ scope.row.fromPackDescribe }}</span>
            <br />
            <span> {{ scope.row.toPackDescribe }}</span>
          </template>
        </el-table-column> -->
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="createUserName"
          label="创建人"
          width="120"
        >
          <template slot-scope="scope">
            <el-button
              @click="
                $EventBus.$emit(
                  'showPublicModal',
                  scope.row.createUserId,
                  'account'
                )
              "
              type="text"
              >{{ scope.row.createUserName }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          sortable
          show-overflow-tooltip
          prop="createTime"
          label="事务时间"
          width="150"
        >
          <template slot-scope="scope">
            {{ scope.row.createTime.substr(5, 11) }}
          </template>
        </el-table-column>

        <!-- <el-table-column sortable prop="billType" label="单据类型" width="100">
          <template slot-scope="scope">
            <span
              v-for="(item, index) in billTypes"
              :key="index"
              v-if="scope.row.billType === item.value"
              >{{ item.label }}</span
            >
          </template>
        </el-table-column> -->

        <el-table-column
          sortable
          show-overflow-tooltip
          prop="billNo"
          label="关联单据号"
          width="180"
        >
        </el-table-column>

        <el-table-column
          sortable
          show-overflow-tooltip
          prop="state"
          label="状态"
          width="80"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.state === 1">完成</span>
            <span v-if="scope.row.state === -1">撤回</span>
          </template>
        </el-table-column>

        <el-table-column
          sortable
          show-overflow-tooltip
          prop="reason"
          label="原因"
          width="100"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.reason === null">-</span>
            <span v-else>{{ scope.row.reason }}</span>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination-bottom"
        style="text-align: right"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        :page-sizes="[15, 30, 50, 100]"
        :current-page.sync="searchData.pageNum"
        :page-size="searchData.pageSize"
        layout="sizes, total, prev, pager, next, jumper"
        :total="searchData.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  name: "InventoryTransact",
  data() {
    return {
      isMoreSearch: false,

      searchData: {
        pageNum: 1,
        pageSize: 15,
        total: null,
        keyWords: "",
        wareId: PF.getLocal("wareId", "number"),
        transactType: "",
        itemCode: "",
        itemName: "",
        billType: "",
        detailNo: "",
        state: "",
        reason: "",
        boxCode: "",
        batchName: "",
        fromBatchName: "",
        fromBoxCode: "",
        fromCellCode: "",
        fromPackDescribe: "",
        fromQuantity: "",
        toBatchName: "",
        toBoxCode: "",
        toCellCode: "",
        toPackDescribe: "",
        toQuantity: "",
        createUserName: "",
        createTimeArr: [],
        startCreateTime: "",
        endCreateTime: "",
      },

      inventoryTransacts: [],

      dialogNewVisible: false,
      dialogUpdateVisible: false,

      formLabelWidth: "120px",
    };
  },
  computed: {
    ...mapState(["transactTypes", "billTypes"]),
  },
  methods: {
    handleSizeChange(val) {
      this.searchData.pageSize = val;
      this.getInventoryTransactData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getInventoryTransactData();
    },
    clearSearchData() {
      this.searchData.keyWords = "";
      this.searchData.itemCode = "";
      this.searchData.itemName = "";
      this.searchData.transactType = "";
      this.searchData.buyType = "";
      this.searchData.detailNo = "";
      this.searchData.state = "";
      this.searchData.batchName = "";
      this.searchData.boxCode = "";
      this.searchData.fromCellCode = "";
      this.searchData.fromPackDescribe = "";
      this.searchData.fromQuantity = "";
      this.searchData.toBatchName = "";
      this.searchData.toBoxCode = "";
      this.searchData.toCellCode = "";
      this.searchData.toPackDescribe = "";
      this.searchData.toQuantity = "";
      this.searchData.createUserName = "";
      this.searchData.createTimeArr = [];
      this.searchData.startCreateTime = "";
      this.searchData.endCreateTime = "";
    },
    getInventoryTransactData() {
      let searchData = PF.JSON(this.searchData);
      if (searchData.createTimeArr.length > 0) {
        searchData.startCreateTime = searchData.createTimeArr[0];
        searchData.endCreateTime = searchData.createTimeArr[1];
      }
      IOT.getServerData(
        "/inventory/transacts/list",
        "get",
        searchData,
        (ret) => {
          if (ret.code === 200) {
            this.inventoryTransacts = ret.rows;
            // console.log(PF.JSON(this.inventoryTransacts));
            this.inventoryTransacts.forEach(item => {
              if(item.transactType == 7){
                item.fromQuantity = item.fromQuantity+"(合格)";
                item.toQuantity = item.toQuantity+"不合格";
              }
            });
            this.searchData.total = ret.total;
            this.searchData.pageNum = ret.pageNumber;
          } else {
            IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
          }
        }
      );
    },
  },
  created() {
    this.getInventoryTransactData();
  },
  mounted() {},
};
</script>

<style scoped lang="less">
.inventoryTransact {
}
</style>
